//
//  MainViewController.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/10/16.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "ProdfileTableViewController.h"
#import "DiscoverTableViewController.h"
#import "MessageTableViewController.h"
#import "MainTabbar.h"
@interface MainViewController ()<UITabBarDelegate>
@property(nonatomic,assign)int indexNumber;

@end

@implementation MainViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.viewControllers = [MainViewController addchildViewControllers];
    self.tabBar.tintColor = [UIColor redColor];
    //    NSLog(@"----%@------",self.tabBar.classForCoder);
    //创建相应的子控制器
    MainTabbar *maintabbar = [[MainTabbar alloc]init];
//    将系统的tabar 更换成为了自定义的 maintabbar
    [self setValue:maintabbar forKey:@"tabBar"];
//    NSLog(@"----%@------",self.tabBar.classForCoder);
    SEL selector = NSSelectorFromString(@"plusBtnDidClick");
    [maintabbar.plusBtn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
//    [self plusBtnDidClick:maintabbar.plusBtn];
}

- (void)plusBtnDidClick{
    NSLog(@"plusBtnDidClick");
    ComposeViewController *composeViewController = [[ComposeViewController alloc]init];
    
    BaseNavViewController *baseNavViewController = [[BaseNavViewController alloc]initWithRootViewController:composeViewController];
    
    [self presentViewController:baseNavViewController animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


+(NSArray *)addchildViewControllers{
    
    HomeTableViewController *hvc = [[HomeTableViewController alloc]init];
    ProdfileTableViewController *pvc = [[ProdfileTableViewController alloc]init];
    DiscoverTableViewController *dvc = [[DiscoverTableViewController alloc]init];
    MessageTableViewController *Mvc = [[MessageTableViewController alloc]init];
    
    [MainViewController mainViewControllerWithRootViewController:hvc title:@"首页" imageName:@"tabbar_home" index:0];
    [MainViewController mainViewControllerWithRootViewController:Mvc title:@"消息" imageName:@"tabbar_message_center" index:1];
    [MainViewController mainViewControllerWithRootViewController:dvc title:@"发现" imageName:@"tabbar_discover" index:2];
    [MainViewController mainViewControllerWithRootViewController:pvc title:@"我" imageName:@"tabbar_profile" index:3];
    
    BaseNavViewController *hvccontroller = [[BaseNavViewController alloc]initWithRootViewController:hvc];
    BaseNavViewController *Mvccontroller = [[BaseNavViewController alloc]initWithRootViewController:Mvc];
    BaseNavViewController *dvccontroller = [[BaseNavViewController alloc]initWithRootViewController:dvc];
    BaseNavViewController *pvccontroller = [[BaseNavViewController alloc]initWithRootViewController:pvc];
    return  @[hvccontroller,Mvccontroller,dvccontroller,pvccontroller];
}

+(void)mainViewControllerWithRootViewController:(UITableViewController *)ViewController title:(NSString *)title imageName:(NSString *)imageName index:(int)index{
    
    BaseNavViewController *navcontroller = [[BaseNavViewController alloc]initWithRootViewController:ViewController];
    
    navcontroller.tabBarItem.badgeValue=@"2";
    ViewController.navigationItem.title = title;
    ViewController.tabBarItem.title = title;
    ViewController.tabBarItem.tag = index;
    ViewController.tabBarItem.image = [[UIImage imageNamed:imageName]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ViewController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //    切记tintcolor的设定是图标下面的文字的颜色一种简便的做法
    
    //   切记,只有在状态为UIControlStateNormal的时候,字体大小的设定才有启动作用.
    //    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:254.0/255 green:1.0/255 blue:1.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateSelected];
    //    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1.0/255 green:254.0/255 blue:1.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:20.0],NSFontAttributeName,nil] forState:UIControlStateNormal];    
}


//实现tabbar被点击时的代理方法,完成动画效果

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    self.indexNumber = 0;
    
    for (UITabBar *v in tabBar.subviews) {
        
        if ([v isKindOfClass:NSClassFromString(@"UITabBarButton")] && v!= nil) {
            
         //   NSLog(@"v = %@",[v class]);
            
            if (item.tag == self.indexNumber) {
                
                for (UIView *subView in v.subviews) {
                    
                    if ([subView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")] && subView!= nil) {
                        subView.transform = CGAffineTransformMakeScale(0.5, 0.5);

                        [UIView animateWithDuration:0.27 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            // 完成形变的操作
                            subView.transform = CGAffineTransformIdentity;

                        } completion:^(BOOL finished) {
                            NSLog(@"动画完成");
                        }];
                        
                    }
                }
                
                
            }
            self.indexNumber ++;
        }
    }
    
    
    
}

@end
